﻿using Sys.Core;
using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Web;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;
using System.ComponentModel.DataAnnotations;

namespace Sys.Controllers
{
    [NoCache, ValidationFilter, Area(GlobalConstants.AREA_NAME)]
    public class OrgController : AuthorizedController<IOrgService>
    {
        [HttpPost]
        public Response<ResultObject<IEnumerable<OrganizationVo>>> Query([FromBody] PagedQueryObject<OrgQuery> request)
        {
            var result = this.Component.Query(request);
            return result.CreateResponse();
        }

        [HttpGet]
        public Response<OrganizationVo> Item([FromQuery, Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string id)
        {
            var result = this.Component.Item(id);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<OrganizationVo> Create([FromBody]OrganizationDto request)
        {
            var result = this.Component.Create(request);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<OrganizationVo> Update([FromBody] OrganizationDto request)
        {
            var result = this.Component.Update(request);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<string> Delete([FromBody] string orgId)
        {
            string result = this.Component.Delete(orgId);
            return result.CreateResponse();
        }

        [HttpGet]
        public Response<ITreeNode> Depts([FromQuery] string orgId)
        {
            var treeNode = this.Component.DeptTree(orgId);
            return treeNode.CreateResponse();
        }

        [HttpPost]
        public Response<DeptVo> CreateDept([FromBody] DeptDto dept)
        {
            var result = this.Component.CreateDept(dept);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<DeptVo> UpdateDept([FromBody] DeptDto dept)
        {
            var result = this.Component.UpdateDept(dept);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<string> DeleteDept([FromBody] string deptPath)
        {
            var result = this.Component.DeleteDept(deptPath);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<ResultObject<IEnumerable<UserVo>>> GetUsers([FromQuery] string orgId, [FromQuery] string deptId, [FromBody] PagedQueryObject<UserQuery> query)
        {
            var result = this.Component.GetUsers(orgId, deptId, query);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<ResultObject<IEnumerable<UserVo>>> ExclusionUsers([FromQuery] string orgId, [FromQuery] string deptId, [FromBody] PagedQueryObject<UserQuery> query)
        {
            var result = this.Component.ExclusionUsers(orgId, deptId, query);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response AddUsers([FromBody] DeptUserSet deptUserSet)
        {
            this.Component.AddUsers(deptUserSet);
            return new();
        }

        [HttpPost]
        public Response RemoveUsers([FromBody] DeptUserSet deptUserSet)
        {
            this.Component.RemoveUsers(deptUserSet);
            return new();
        }
    }
}
